package com.kuang.volatile_t;

import java.util.concurrent.TimeUnit;

/**
 * @program: juc
 * @description:
 * @author: 陈恩涛
 * @create: 2021-03-25 10:24
 **/
public class JMMDemo {
    // 不加 volatile程序就会死循环
    // 加 volatile 可以保证可见性
    private static volatile int num = 0;

    public static void main(String[] args) {
        //主线程

        //线程1
        new Thread(() -> {
            while (num == 0){

            }
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        num = 1;
        System.out.println(num);
    }
}
